package idv.takeshi.software.productline.bookstore.domain.model.deliveryservice;

import java.math.BigDecimal;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.apache.commons.lang.Validate;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * A abstract model class represents as a Delivery Service.
 * @author takeshi
 *
 */
@Entity
@Table(name = "DELIVERY_SERVICE")
public class DeliveryService {
	
	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	private Integer id;
	
	@Column(nullable = false, length = 50)
	private String name;
	
	@Column(nullable = false, name = "DELIVERY_FEE")
	private BigDecimal deliveryFee;
	@Column(nullable = false, name = "DELIVERY_FEE_OFF_THRESHOLD") 
	private BigDecimal deliveryFeeOffThreshold;
	
	@Column(nullable = false, name = "CREATE_TIME")
	private Date createTime;
	
	@Column(nullable = false, name = "UPDATE_TIME")
	private Date updateTime;
	
	/**
	 * A postal or convenient store service.
	 */
	@Column(nullable = false)
	private boolean postal;
	
	/**
	 * Test given price is whether greater than threshold for off the delivery fee. 
	 * @param price
	 * @return
	 */
	public boolean isOverDeliveryFeeOffThreshold(BigDecimal price){
		Validate.notNull(price, "price shall always not be null");
		int value = 0;
		if(null != deliveryFeeOffThreshold){
			value = price.compareTo(deliveryFeeOffThreshold);
		}
		return (value >= 0);
	}
	
	/**
	 * Add delivery fee for the given price.
	 * @param price
	 * @return
	 */
	public BigDecimal addDeliveryFee(BigDecimal price){
		Validate.notNull(price, "price shall always not be null");
		BigDecimal newPrice = price;
		if(!this.isOverDeliveryFeeOffThreshold(newPrice) &&  null != deliveryFee){
			newPrice = price.add(deliveryFee);
		}
		return newPrice;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the deliveryFee
	 */
	public BigDecimal getDeliveryFee() {
		return deliveryFee;
	}

	/**
	 * @param deliveryFee the deliveryFee to set
	 */
	public void setDeliveryFee(BigDecimal deliveryFee) {
		this.deliveryFee = deliveryFee;
	}


	/**
	 * @return the deliveryFeeOffThreshold
	 */
	public BigDecimal getDeliveryFeeOffThreshold() {
		return deliveryFeeOffThreshold;
	}

	/**
	 * @param deliveryFeeOffThreshold the deliveryFeeOffThreshold to set
	 */
	public void setDeliveryFeeOffThreshold(BigDecimal deliveryFeeOffThreshold) {
		this.deliveryFeeOffThreshold = deliveryFeeOffThreshold;
	}

	/**
	 * @return the postal
	 */
	public boolean isPostal() {
		return postal;
	}

	/**
	 * @param postal the postal to set
	 */
	public void setPostal(boolean postal) {
		this.postal = postal;
	}

	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	/**
	 * @return the updateTime
	 */
	public Date getUpdateTime() {
		return updateTime;
	}

	/**
	 * @param updateTime the updateTime to set
	 */
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null) { return false; }
		   if (obj == this) { return true; }
		   if (obj.getClass() != getClass()) {
		     return false;
		   }
		   DeliveryService rhs = (DeliveryService) obj;
		   return new EqualsBuilder()
			   .append(this.id, rhs.id)
			   .append(this.name, rhs.name)
			   .append(this.deliveryFee, rhs.deliveryFee)
			   .append(this.deliveryFeeOffThreshold, rhs.deliveryFeeOffThreshold)
			   .append(this.postal, rhs.postal)
			   .append(this.createTime, rhs.createTime)
			   .append(this.updateTime, rhs.updateTime)
		       .isEquals();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder(61, 67).
	       append(this.id).
	       append(this.name).
	       append(this.deliveryFee).
	       append(this.deliveryFeeOffThreshold).
	       append(this.postal).
	       append(this.createTime).
	       append(this.updateTime).
	       toHashCode();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
	}
	 
}
 
